{include file='header'/}
<style>
    .layui-breadcrumb {visibility: visible;}
</style>
<body>
<div class="weadmin-nav">
			<span class="layui-breadcrumb">
       		 <a href="">首页</a><span lay-separator="">/</span>
        	<a href="">常规管理</a><span lay-separator="">/</span>
            <a><cite>数据库管理</cite></a>
      </span>
    <a class="layui-btn layui-btn-sm" style="float:right;margin-top: 5px" href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon">&#xe9aa;</i></a>
</div>
<div class="weadmin-body">

    <div class="page-container">
        <form method="post" name="myform" id="myform" action="{:url('export_list')}">
            <div class="weadmin-block demoTable">
                <a class="layui-btn layui-btn-normal"  data-type="beifenData">立即备份</a>
                <a class="layui-btn"  data-type="youhuaData">优化表</a>
                <a class="layui-btn layui-btn-danger" data-type="xiufuData">修复表</a>
                <a class="layui-btn layui-btn-fense" data-type="huanyuanData">数据还原</a>
                <span class="fr" style="line-height:40px;padding-right: 10px;">共有数据：<span  id="total"></span> 条</span>
            </div>
            <table id="database_list" lay-filter="database_list"></table>
            <script type="text/html" id="barDemo">
                <a class="layui-btn layui-btn-sm" lay-event="youhua">优化</a>
                <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="xiufu">修复</a>
                <a class="layui-btn layui-btn-normal layui-btn-sm" lay-event="jiegou">表结构</a>
                <a class="layui-btn layui-btn-normal layui-btn-sm layui-btn-lvse" lay-event="shuju">表数据</a>
            </script>
        </form>
    </div>
</div>

{include file='footer'/}
<script>
    layui.use('table', function(){
        var table = layui.table;
        //第一个实例
        table.render({
            elem: '#database_list'
            ,page: false
            ,method: 'get'
            ,url: '{:url('index',['do'=>1])}' //数据接口
            ,done: function(res){ //res 即为原始返回的数据
                $("#total").html(res.count);
            }
            ,cols: [[ //表头
                {type:'checkbox',LAY_CHECKED:true}
                ,{field: 'Name', title: '表名',width:200}
                ,{field: 'Engine', title: '类型'}
                ,{field: 'Collation', title: '编码'}
                ,{field: 'Rows', title: '数据量',width:100}
                ,{field: 'Data_length', title: '数据大小'}
                ,{field: 'Create_time', title: '创建时间'}
                ,{field: 'Data_free', title: '碎片'}
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo',width:280,align:'center'}
            ]]
            ,id:'Datalist'
            ,page:false
            ,limit:10000
        });
        //监听行工具事件
        table.on('tool(database_list)', function(obj){
            var data = obj.data;
            if(obj.event === 'youhua'){
                getevent("{:url('optimize')}",data);
            } else if(obj.event === 'xiufu'){
                getevent("{:url('repair')}",data);
            }else if(obj.event === 'jiegou'){
                WeAdminShow("查看表结构","{:url('viewinfo')}?table="+data.Name,'800','550');
            }else if(obj.event === 'shuju'){
                WeAdminShow("查看表数据","{:url('viewdata')}?table="+data.Name,'800','600');
            }
        });

        var $ = layui.$, active = {
            youhuaData: function(){ //优化表数据
                var tables = table_get_ids(table);
                if(tables===false){
                    return false;
                }
                var loading = layer.load();
                $.post("{:url('optimize')}",{
                    table:tables
                },function (res) {
                    layer.close(loading);
                    layer.msg(res.message,{
                        icon:res.icon
                    })
                })

            }
            ,xiufuData: function(){ //修复表数据
                var tables = table_get_ids(table);
                if(tables===false){
                    return false;
                }
                var loading = layer.load();
                $.post("{:url('repair')}",{
                    table:tables
                },function (res) {
                    layer.close(loading);
                    layer.msg(res.message,{
                        icon:res.icon
                    })
                })
            }
            ,beifenData: function(){ //立即备份
                var loading = layer.load();
                $.post("{:url('backup')}", {
                    dosubmit:1,
                }, function(str){
                    layer.close(loading);
                    layer.msg(str.message,{time:1000},function () {
                        if(str.status==1){
                            WeAdminShow("数据还原","{:url('databack_list')}",'880','550');
                        }
                    });
                });
            }
            ,huanyuanData:function () {//备份数据列表
                WeAdminShow("数据还原","{:url('databack_list')}",'880','550');
            }
        };
        $('.demoTable .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });
    function getevent(url,data) {
        $.get(url,{
            table:data.Name
        },function (res) {
            layer.msg(res.message,{
                icon:res.icon
            })
        })
    }

    function table_get_ids(table) {
        var checkStatus = table.checkStatus('Datalist')
            ,data = checkStatus.data;
        if(data.length<1){
            layer.msg('你没有选择任何数据');
            return false;
        }else{
            var ids=[];
            for(var i=0;i<data.length;i++){
                ids +=data[i].Name+',';
            }
            ids =ids.slice(0, -1);
            var arr =ids.split(',');
            for(var i in arr){
                (arr[i])
            }
            return arr;
        }
    }
</script>
</body>
</html>